/**
 * 
 */
package t4j.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author sz
 *
 * create time：2013-2-28
 */
public class EvenChecker implements Runnable{
	private IntGenerator generator;
	private final int id;
	
	public EvenChecker(IntGenerator g,int ident) {
		generator = g;
		id = ident;
	}
	
	public void run(){
		while(!generator.isCanceled()){
			int val = generator.next();
			
			if(val % 2 != 0){//不是偶数
				System.out.println("id = " +id +","+val + " not even!");
				generator.cancel();
			}
			
		}
	}
	
	public static void test(IntGenerator gp,int count){
		System.out.println("Press Control-C to exit");
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		for(int i = 0 ; i < count ; i++)
			exec.execute(new EvenChecker(gp, i));//10个线程同时跑一个IntGenerator子类
		
		exec.shutdown();
	}
	
	public static void test(IntGenerator gp){
		test(gp,10);
	}
}
